//
//  CoGeMoviePlayerPlugIn.h
//  CoGeMoviePlayer
//
//  Created by Tamas Nagy on 1/21/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Quartz/Quartz.h>
#import <QuickTime/QuickTime.h>
#import <QTKit/QTKit.h>
#import "HapSupport.h"
#import "HapPixelBufferTexture.h"
#import "CoGeMoviePreload.h"
#import "CoGeMovieThreadSafeMutableDictionary.h"

@interface CoGeMoviePlayerPlugIn : QCPlugIn
{
	
	QTMovie	*_movie;
	CVPixelBufferRef _latestTextureFrame;
	QTVisualContextRef _visualContext;

    QTVisualContextRef _visualContextTexture;
    QTVisualContextRef _visualContextHAP;

	id provider;
	
	NSSize movieSize;
	
	int currentplayback, currentbank;
	
	NSMutableDictionary *movieData;
	
	BOOL needstrigger;
	
	float workingStartPoint;
	float workingEndPoint;
	float workingVolume;
	float workingRate;
	int workingLoopMode;
	
	BOOL newstartpointset;
	
	BOOL movieOKToPlayBack;
	
	BOOL movieChanged;
	
	BOOL movieFinished;
	
	
	BOOL waitForTriggerNextSlot;
	
	
	BOOL shouldProcessBecauseMediaBankChanged;
	
	NSDictionary *moviePathes;
	
	CGLContextObj _context;
	
	QTMovie *currentmovie;
    
    BOOL skipNextFrame;
    
    BOOL shouldStartMovie;
	BOOL readyToSetPlayhead;
    
    BOOL isPlaybackHAPMovie;
    
    HapPixelBufferTexture *hapTexture;
    
    NSInteger nextSlotToTrigger;
    
    BOOL movieended;
    
    NSString *defAudioDevice;
    
    CoGeMovieThreadSafeMutableDictionary *preloadData;
    NSOperationQueue *preloadQueue;
    
    BOOL isUsingPreloadedMedia;
    
    NSMutableDictionary *preloadCache;
    
    CoGeMovieThreadSafeMutableDictionary *preloadstatechanged;
}

@property (assign) NSInteger nextSlotToTrigger;
@property (readwrite, retain) NSDictionary *moviePathes;
@property (assign) NSDictionary * inputCoGeLayerFilePathes;
@property (assign) NSUInteger inputMovieIndex;
@property (assign) NSUInteger inputLoopMode;
@property (assign) NSString * inputAudioDeviceUID;
@property (assign) double outputSlotMarkedToTrigger;
@property (assign) id <QCPlugInOutputImageProvider> outputImage;
@property (assign) 	BOOL inputSlotClicked;
@property (assign) 	BOOL inputNoResetOnTrigger;
@property (assign) 	BOOL inputIsActive;
@property (assign) 	BOOL inputBankChanged;
@property (assign) 	BOOL inputPlayHeadSetByMouse;
@property (assign) 	BOOL outputMovieReachedEndPoint;
@property (assign) 	BOOL outputMoviePlayerInited;
@property (readwrite, retain) NSMutableDictionary *movieData;
@property (readwrite, assign) float workingStartPoint;
@property (readwrite, assign) float workingEndPoint;
@property (readwrite, assign) float workingVolume;
@property (readwrite, assign) float workingRate;
@property (readwrite, assign) int workingLoopMode;
@property (assign) 	double inputRate;
@property (assign) 	double inputPlayhead;
@property (assign) 	double inputVolume;
@property (assign) 	double inputStartPoint;
@property (assign) 	double inputEndPoint;
@property (assign, readwrite) NSSize movieSize;
@property (assign, readwrite) BOOL movieOKToPlayBack;
@property (assign) double outputMovieTime;
@property (readwrite, assign) BOOL movieChanged;
@property (assign) 	BOOL shouldStartMovie;
@property (assign) 	BOOL readyToSetPlayhead;
@property (assign) 	BOOL isPlaybackHAPMovie;
@property (assign) 	BOOL isUsingPreloadedMedia;
@property (assign) NSDictionary *outputPreloadStateChanges;
@property (assign) 	BOOL inputPreloadMovies;

-(void)setPreloadedMovie:(QTMovie *)movie onBank:(int)bank onSlot:(int)slot;

/*
 Declare here the Obj-C 2.0 properties to be used as input and output ports for the plug-in e.g.
 @property double inputFoo;
 @property(assign) NSString* outputBar;
 You can access their values in the appropriate plug-in methods using self.inputFoo or self.inputBar
 */
-(BOOL)isCurrentMoviePlayable;
-(double)movieNormalizedTime;

@end

@interface CoGeMovieImageProvider : NSObject <QCPlugInOutputImageProvider>
{
	NSRect imageBounds;
	GLfloat lowerLeft[2],lowerRight[2],upperRight[2],upperLeft[2];
	GLenum target;
	GLuint textureName;
    NSSize textureSize;
    NSSize imageSize;
    BOOL isFlipped;
    GLhandleARB shader;
    GLint previousFBO;
    GLint previousReadFBO;
    GLint previousDrawFBO;

    CGColorSpaceRef _colorspace;
    
}
-(id)initWithTexture:(GLuint)_tex target:(GLenum)_target imageSize:(NSSize)_size textureSize:(NSSize)_texsize flipped:(BOOL)flip shader:(GLhandleARB)_shader;

// required for protocol compliance
//- (NSArray*) supportedRenderedTexturePixelFormats;
- (BOOL) shouldColorMatch;
- (NSRect) imageBounds;
- (CGColorSpaceRef) imageColorSpace;

// required for using OpenGL providers
- (BOOL) canRenderWithCGLContext:(CGLContextObj)cgl_ctx;
//- (void) releaseRenderedTexture:(GLuint)name forCGLContext:(CGLContextObj)cgl_ctx;
- (BOOL) renderWithCGLContext:(CGLContextObj)cgl_ctx forBounds:(NSRect)bounds;

// the call below returns a texture for the provider. Its either renderWithCGLContext or the method below.
//- (GLuint) copyRenderedTextureForCGLContext:(CGLContextObj)cgl_ctx pixelFormat:(NSString*)format bounds:(NSRect)bounds isFlipped:(BOOL*)flipped

@end
